/**
 * 
 */
package com.edu.lawdoc.api.controller.assistor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.edu.lawdoc.i18n.I18nDelegate;
import org.apache.commons.lang3.StringUtils;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;

/**
 * @author hu.xiao
 *
 */
public class Result {
	public static final String EMPTY = "";
	
	public final static String IS_OK = "isOK";
	public final static String ERROR_CODE = "errorCode";
	public final static String DATA = "data";
	public final static String MESSAGE = "message";
	
	/**
	 * | "201" ; Created
	 * | "202" ; Accepted
	 * | "204" ; No Content
	 * | "301" ; Moved Permanently
	 * | "302" ; Moved Temporarily
	 * | "304" ; Not Modified
	 * | "400" ; Bad Request
	 * | "401" ; Unauthorized 401： 表示未经授权,
	 * | "403" ; Forbidden
	 * | "404" ; Not Found
	 * | "500" ; Internal Server Error
	 * | "501" ; Not Implemented
	 * | "502" ; Bad Gateway
	 * | "503" ; Service Unavailable
	 * 
	 * @param oController
	 */
	public static void render401(Controller oController) {
		oController.getResponse().setStatus(401);
		oController.renderNull();
	}
	
	/**
	 * 统一返回结果的方法，不要再使用其它形式返回结果
	 * @param oController
	 * @param errorCode
	 * @param message
	 * @param data
	 */
	private static void result(Controller oController, boolean isOK, String errorCode, String message, Object data) {
		if(StringUtils.isNotBlank(errorCode) && StringUtils.isBlank(message)) {
			message = I18nDelegate.get(errorCode);
		}
		
		oController.setAttr(IS_OK, isOK);
		oController.setAttr(ERROR_CODE, errorCode == null ? EMPTY : errorCode);
		oController.setAttr(MESSAGE, message == null ? EMPTY : message);
		oController.setAttr(DATA, data == null ? EMPTY : data);
		oController.renderJson(new String[]{IS_OK, ERROR_CODE, MESSAGE, DATA});
	}

	/**
	 * 返回操作成功的结果
	 * @param oController
	 */
	public static void success(Controller oController, Object data) {
		result(oController, true, null, null, data);
	}
	
	public static void success(Controller oController) {
		result(oController, true, null, null, null);
	}
	
	/**
	 * 返回操作失败的结果
	 * @param oController
	 * @param errorCode
	 * @param message
	 * @param data
	 */
	public static void failure(Controller oController, String errorCode, String message, Object data) {
		result(oController, false, errorCode, message, data);
	}
	
	public static void failure(Controller oController, String errorCode, String message) {
		result(oController, false, errorCode, message, null);
	}	
	
	public static void failure(Controller oController, String errorCode) {
		result(oController, false, errorCode, null, null);
	}
	
	/**
	 * 组装返回给页面的列表数据
	 * @param oPage
	 * @return
	 */
	public static DataList packDataList(Page<?> oPage) {
		return packDataList(oPage, null);
	}
	
	public static DataList packDataList(Page<?> oPage, Map<String, Map<String, String>> dict) {
		DataList oDataList = new DataList();
		oDataList.setData(oPage.getList());
		oDataList.setCount(oPage.getTotalRow());
		oDataList.setDict(dict);
		return oDataList;
	}
	
	public static DataList packDataList(List<?> list) {
		DataList oDataList = new DataList();
		oDataList.setData(list);
		oDataList.setCount(list.size());
		return oDataList;
	}
	
	/**
	 * 为返回空的查询数据而组装的结果
	 * @return
	 */
	public static DataList packEmptyDataList() {
		DataList oDataList = new DataList();
		oDataList.setData(new ArrayList<Object>());
		oDataList.setCount(0);
		return oDataList;
	}
}
